package com.etcsoft.examples.snippet;

import java.util.ArrayList;

import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

public class Snippet027TreeLayout {
    private class MyContentProvider implements ITreeContentProvider {

        @Override
        public Object[] getElements(Object inputElement) {
            return ((MyModel) inputElement).child.toArray();
        }

        @Override
        public void dispose() {

        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

        }

        @Override
        public Object[] getChildren(Object parentElement) {
            return this.getElements(parentElement);
        }

        @Override
        public Object getParent(Object element) {
            if (element == null) {
                return null;
            }

            return ((MyModel) element).parent;
        }

        @Override
        public boolean hasChildren(Object element) {
            return ((MyModel) element).child.size() > 0;
        }

    }

    public class MyModel {
        public MyModel parent;

        public ArrayList<MyModel> child = new ArrayList<MyModel>();

        public int counter;

        public MyModel(int counter, MyModel parent) {
            this.parent = parent;
            this.counter = counter;
        }

        @Override
        public String toString() {
            String rv = "Item ";
            if (this.parent != null) {
                rv = this.parent.toString() + ".";
            }

            rv += this.counter;

            return rv;
        }
    }

    public class MyLabelProvider extends LabelProvider implements
            ITableLabelProvider, ITableFontProvider, ITableColorProvider {
        FontRegistry registry = new FontRegistry();

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(Object element, int columnIndex) {
            return "Column " + columnIndex + " => " + element.toString();
        }

        @Override
        public Font getFont(Object element, int columnIndex) {
            if (((MyModel) element).counter % 2 == 0) {
                return this.registry.getBold(Display.getCurrent().getSystemFont()
                        .getFontData()[0].getName());
            }
            return null;
        }

        @Override
        public Color getBackground(Object element, int columnIndex) {
            if (((MyModel) element).counter % 2 == 0) {
                return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
            }
            return null;
        }

        @Override
        public Color getForeground(Object element, int columnIndex) {
            if (((MyModel) element).counter % 2 == 1) {
                return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
            }
            return null;
        }

    }

    public Snippet027TreeLayout(Shell shell) {
        final TreeViewer v = new TreeViewer(shell);
        v.getTree().setHeaderVisible(true);
        v.getTree().setLinesVisible(true);

        TreeColumnLayout ad = new TreeColumnLayout();
        shell.setLayout(ad);

        TreeColumn column1 = this.createTreeColumn(v.getTree(), "Column 1");
        TreeColumn column2 = this.createTreeColumn(v.getTree(), "Column 2");

        ad.setColumnData(column1, new ColumnWeightData(50, 100));
        ad.setColumnData(column2, new ColumnWeightData(50, 100));

        v.setLabelProvider(new MyLabelProvider());
        v.setContentProvider(new MyContentProvider());
        v.setInput(this.createModel());
    }

    private TreeColumn createTreeColumn(Tree tree, String textColumn) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText(textColumn);
        column.setWidth(200);
        return column;
    }

    private MyModel createModel() {

        MyModel root = new MyModel(0, null);
        root.counter = 0;

        MyModel tmp;
        for (int i = 1; i < 10; i++) {
            tmp = new MyModel(i, root);
            root.child.add(tmp);
            for (int j = 1; j < i; j++) {
                tmp.child.add(new MyModel(j, tmp));
            }
        }

        return root;
    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        new Snippet027TreeLayout(shell);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }
}
